package com.java.shop.order.web;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.java.shop.order.service.OrderService;
import com.java.shop.order.vo.PayAsyncVo;
import com.java.shop.order.vo.PayOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PayWebController {
    @Value("${alipay.returnUrl}")
    private String returnUrl;

    @Autowired
    OrderService orderService;

    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String pay(@RequestParam("orderSn") String orderSn) throws Exception {
        PayOrderVo vo = orderService.getPayOrder(orderSn);
        AlipayTradePagePayResponse response = Factory.Payment.Page()
                .optional("timeout_express","1m")
                .pay(vo.getSubject(), vo.getOutTradeNo(), vo.getTotalAmount(), returnUrl);
        return response.getBody();
    }

    @ResponseBody
    @PostMapping("/payed/notify")
    public String payedNotify(PayAsyncVo vo) throws Exception {
        //Alipay Easy SDK（新版）中的方法已经涵盖公钥证书和普通公钥签名方式
        return orderService.handlePayResult(vo);
    }
}
